#include<test_data_recorder.h>

TestDataRecorder::TestDataRecorder(std::string path){//打开文件，获取pid
    // pid = getpid();
    // std::string filename = path+"/middle"+std::to_string(pid)+".txt";//p要打开的文件名
    this->path = path;
    // outfile.open(filename,std::ios::out | std::ios::app);  // 创建并打开文件
    // if (!outfile.is_open()) {          // 检查是否成功打开
    //     std::cout << "Failed to create file." << std::endl;
    //     return;
    // }
    // outfile.close();

}
//p写入
bool TestDataRecorder::write(std::string name1, std::string name2, std::string key1, std::string val1, std::string key2, std::string val2, std::string key3, std::string val3){
    int pid = getpid();
    std::string sem_name = "/"+std::to_string(10086);//记得改回来
    //加锁
    sem_t *sem = sem_open(sem_name.c_str(), O_CREAT, 0644, 1); // 打开信号量
    if (sem == SEM_FAILED) {
        perror("sem_open");
    }
    sem_wait(sem); // 等待信号量
    ////////
//记得改回来
    std::string filename = path+"/middle"+/*std::to_string(pid)*/"10086"+".txt";//p要打开的文件名
    std::fstream outfile(filename,std::ios::out | std::ios::app);  //打开文件
    if (!outfile.is_open()) {          // 检查是否成功打开
        std::cout << "Failed to open file.\n" << std::endl;
        return false;
    }
    //要写入中间文件的字符串
    std::string temp = slice1+name1+slice2+name2+slice3+key1+slice4+val1+slice5+key2+slice6+val2+slice7+key3+slice8+val3+"\n";//slice9+slice10+slice11;
    outfile<<temp;
    outfile.close();
    //解锁
    sem_post(sem); // 释放信号量
    sem_close(sem); // 关闭信号量描述符
    return true;
}
// bool TestDataRecorder::write(std::string name1, std::string name2, std::string key1, std::string val1, std::string key2, std::string val2, std::string key3, std::string val3){
//     // 尝试获取文件锁  
//     int fd = open("data.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);  
//     if (fd == -1) {  
//         perror("open");  
//         return 1;  
//     }  
//     struct flock lock;  
//     lock.l_type = F_WRLCK;  
//     lock.l_start = 0;  
//     lock.l_whence = SEEK_SET;  
//     lock.l_len = 0; // 锁定整个文件  
//     if (fcntl(fd, F_SETLKW, &lock) == -1) {  
//         perror("fcntl");  
//         close(fd);  
//         return 1;  
//     }  
//     // 写入数据  
//     const char* data = "Hello, world!";  
//     std::write(fd, data, std::strlen(data));  
//     // 释放文件锁  
//     lock.l_type = F_UNLCK;  
//     if (fcntl(fd, F_SETLK, &lock) == -1) {  
//         perror("fcntl unlock");  
//         close(fd);  
//         return 1;  
//     }
// }
//executor写入
bool TestDataRecorder::write(std::string front,std::string name1, std::string name2){
    std::string filename = path+"/final.txt";//executor要打开的文件名
    std::fstream outfile(filename,std::ios::out | std::ios::app);  //打开文件
    if (!outfile.is_open()) {          // 检查是否成功打开
        std::cout << "Failed to open file." << std::endl;
        return false;
    }
    //要写入中间文件的字符串
    std::string temp = front+slice9+name1+slice10+name2+slice11+"\n";
    outfile<<temp;
    outfile.close();
    return true;
}
//executor读取
std::vector<std::string> TestDataRecorder::read(int pid){
    //加锁
    std::string sem_name = "/"+std::to_string(pid);
    sem_t *sem = sem_open(sem_name.c_str(), O_CREAT, 0644, 1); // 打开信号量  
    if (sem == SEM_FAILED) {
        perror("sem_open");
    }
    sem_wait(sem); // 等待信号量  
    ////////
    std::string filename = path+"/middle"+std::to_string(pid)+".txt";//executor要打开的文件名
    std::vector<std::string>v;
    std::string line;
    std::ifstream inFile(filename);
	if (!inFile) {
		std::cerr << "无法打开文件！" << std::endl;
        return v;
	}
    while(std::getline(inFile, line)){
	    //line = line.substr(0, line.length() - 1);
        v.push_back(line);
    }
    clear(pid);//将文件内容删除
    //解锁
    sem_post(sem); // 释放信号量  
    sem_close(sem); // 关闭信号量描述符  
    return v;

}

// bool TestDataRecorder::is_content(){
// }
void TestDataRecorder::clear(int pid){
    std::string filename = path+"/middle"+std::to_string(pid)+".txt";
    std::ofstream truncateFile(filename, std::ios::trunc);
	truncateFile.close();
}

TestDataRecorder::~TestDataRecorder(){    
}

